<template>
  <el-container class="m-login">
    <el-header height="80px">
      <el-row>
        <el-col :span="12"
                class="title"
                style="text-align:left;">
          <span style="color:#fff;">雾桐电子烟管理系统</span>
        </el-col>
        <el-col :span="12"
                class="status"></el-col>
      </el-row>
    </el-header>
    <el-main style="height:calc(100vh - 80px)">
      <el-form :model="loginForm"
               :rules="rules"
               ref="loginForm"
               class="loginForm">
        <div class="form-tittle"
             style="padding: 10px 0">系统登录</div>
        <el-form-item label="账号"
                      prop="name"
                      style="padding: 5px 0">
          <el-input v-model="loginForm.name"
                    @keyup.enter.native="submit()"
                    style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="psw">
          <el-input type="password"
                    v-model="loginForm.psw"
                    @keyup.enter.native="submit()"
                    style="width:80%"
                    show-password></el-input>
        </el-form-item>
        <!-- <div class="forget-psd"
             @click="forgetPSD">
          忘记密码
        </div> -->
        <el-form-item style="margin:30px 0 0 0;width:100%;">
          <el-row>
            <el-col :span="12"
                    :offset="12"
                    style="text-align:center; width:100%;margin-top:10px; ">
              <el-button size="medium"
                         type="primary"
                         @click="submit()">登录</el-button>
              <el-button size="medium"
                         @click="resetForm('loginForm')">重置</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { login } from '../../services/services_user'
export default {
  data () {
    return {
      isdisabled: false,
      activeIndex: "1",
      vbgUrl: "",
      loginForm: {
        name: "",
        psw: ""
      },
      rules: {
        name: [
          { required: true, message: "登录账号不能为空", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
        ],
        psw: [
          { required: true, message: "登录密码不能为空", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      },

    };
  },
  mounted () {
    this.vbgUrl = window.baseUrl;
  },
  computed: {},
  methods: {
    submit () {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            const res = await login({
              userName: this.loginForm.name,
              password: window.btoa(this.loginForm.psw)
            })
            if (res.code == 0) {
              this.$message.success('登录成功')
              this.$store.commit('app/updateToken', res.data.token)
              this.$store.commit('app/updateUserId', res.data.userId)
              this.$store.dispatch('app/getUserInfo', res.data.userId)

              this.$router.replace('/home')
            } else {
              if (res.msg) {
                this.$message.warning(res.msg)
              }
            }
          } catch (error) {
            console.log(error);
          }
        }
      })
    },
    resetForm () {
      this.$refs.loginForm.resetFields();
    },
    forgetPSD () {
      this.$router.push('/question')
    }
  },
};
</script>

<style lang="scss" scoped>
.el-container {
  background: url(~@/assets/img/loginbg.jpg) center;
  background-size: cover;
}
.m-login .el-col-offset-12 {
  margin-left: 0;
}
.el-main {
  background: none;
  position: relative;
}
.el-footer {
  background: none;
  color: #fff;
}

.title {
  line-height: 80px;
  font-size: 26px;
  letter-spacing: 2px;
  text-align: center;
  background: transparent;
}
.title span {
  margin-left: 30px;
}
.el-form {
  width: 400px;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  background-color: #fff;
  height: 270px;
  border-radius: 10px;
  padding: 20px 30px;
}
.form-tittle {
  font-size: 18px;
  margin-bottom: 10px;
  letter-spacing: 2px;
}
.forget-psd {
  position: absolute;
  right: 48px;
  top: 202px;
  font-size: 14px;
  cursor: pointer;
}
</style>
